package order_service

import (
	"bee/pkg/logging"
	"bee/pkg/util"
	"github.com/go-resty/resty/v2"
	"strconv"
)

type BaseInfo struct {
	OrderType    string `json:"orderType"`
	PurchaserID  string `json:"purchaserId"`
	ServiceType  string `json:"serviceType"`
	SalesChannel string `json:"salesChannel"`
	TerminalType string `json:"terminalType"`
}

type OrdinaryProduct struct {
	ProductID        string  `json:"productId"`
	ProductVersionID string  `json:"productVersionId"`
	StandardPrice    float64 `json:"standardPrice"`
	MinimumPrice     float64 `json:"minimumPrice"`
	SellingCount     int     `json:"sellingCount"`
	SellingAmount    string `json:"sellingAmount"`
	EffectiveDateStart string `json:"effectiveDateStart"`
	EffectiveDateEnd string `json:"effectiveDateEnd"`
	PresentTag string `json:"presentTag"`
	PresentQuantity string `json:"presentQuantity"`
}

type ProductInfo struct {
	OrdinaryProductList []OrdinaryProduct `json:"ordinaryProductList"`
	ReductionReason string `json:"reductionReason"`
}

type ContactInfo struct {
	ContactName string `json:"contactName"`
	Phone       string `json:"phone"`
	Gender      string `json:"gender"`
	Role        string `json:"role"`
}

type OrderRemark struct {
	SignerID     string `json:"signerId"`
	OperatorType string `json:"operatorType"`
	OperatorID   string `json:"operatorId"`
}

type AccountDeduct struct {
	AccountID       int    `json:"accountId"`
	AccountType     string `json:"accountType"`
	TradeAmount     int    `json:"tradeAmount"`
	AvailableAmount int    `json:"availableAmount"`
}

type BalanceDeductInfo struct {
	CustomerIdentityID  int           `json:"customerIdentityId"`
	CouponDeductAmount  int           `json:"couponDeductAmount"`
	AccountDeductAmount int           `json:"accountDeductAmount"`
	InvoiceAmount       int           `json:"invoiceAmount"`
	CouponDeductList    []interface{} `json:"couponDeductList"`
	AccountDeductList   []AccountDeduct `json:"accountDeductList"`
}

type PayInfo struct {
	PaymentWay        string `json:"paymentWay"`
	BalanceDeductInfo  `json:"balanceDeductInfo"`
}

type OrdinaryOrder struct {
	BaseInfo  `json:"baseInfo"`
	ProductInfo  `json:"productInfo"`
	InvoiceBeforePaid string `json:"invoiceBeforePaid"`
	InvoiceInfo `json:"invoiceInfo"`
	DeliveryInfo  `json:"deliveryInfo"`
	ContactInfo  `json:"contactInfo"`
	OrderRemark  `json:"orderRemark"`
	PayInfo `json:"payInfo"`
}

type NonePayOrdinaryOrder struct {
	BaseInfo  `json:"baseInfo"`
	ProductInfo  `json:"productInfo"`
	InvoiceBeforePaid string `json:"invoiceBeforePaid"`
	InvoiceInfo `json:"invoiceInfo"`
	DeliveryInfo  `json:"deliveryInfo"`
	ContactInfo  `json:"contactInfo"`
	OrderRemark  `json:"orderRemark"`
	NonePayInfo interface{} `json:"payInfo"`
}

func (o Order) CreateOrdinaryOrder(host string) (string, error) {

	if o.OrderType == 0 {
		logging.ZapLogger.Infow("【普通订单】 下单类型为0，不创建普通订单", "traceId", o.TraceId)
		return "", nil
	}

	url := host + "/api/otcpro/v1/order/ordinaryOrder/save"

	baseInfo := BaseInfo{
		OrderType:    "0",
		PurchaserID:  o.CustomerId,
		ServiceType:  "01",
		SalesChannel: "50",
		TerminalType: "1",
	}

	var ordinaryProductList []OrdinaryProduct

	//ordinaryProductA := OrdinaryProduct{
	//	ProductID:        "32575",
	//	ProductVersionID: "56121",
	//	StandardPrice:    0.1,
	//	MinimumPrice:     0.1,
	//	SellingCount:     1,
	//	SellingAmount:    "0.1",
	//	EffectiveDateStart: "2020-08-01",
	//	EffectiveDateEnd: "2021-07-31",
	//	PresentTag: "0",
	//	PresentQuantity: "1",
	//}
	//
	//ordinaryProductB := OrdinaryProduct{
	//	ProductID:        "32574",
	//	ProductVersionID: "56173",
	//	StandardPrice:    11,
	//	MinimumPrice:     11,
	//	SellingCount:     1,
	//	SellingAmount:    "11",
	//	EffectiveDateStart: "2020-08-01",
	//	EffectiveDateEnd: "2021-07-31",
	//	PresentTag: "0",
	//	PresentQuantity: "1",
	//}
	var ordinaryProductC OrdinaryProduct
	ordinaryProductC = OrdinaryProduct{
		ProductID:        "32573",
		ProductVersionID: "56435",
		StandardPrice:    6000,
		MinimumPrice:     6000,
		SellingCount:     1,
		SellingAmount:    "100",
		EffectiveDateStart: "2020-01-01",
		EffectiveDateEnd: "2020-12-31",
		PresentTag: "0",
		PresentQuantity: "1",
	}

	if o.OrderType == 5 {
		ordinaryProductC = OrdinaryProduct{
			ProductID:        "32573",
			ProductVersionID: "56435",
			StandardPrice:    6000,
			MinimumPrice:     6000,
			SellingCount:     1,
			SellingAmount:    "100",
			EffectiveDateStart: "2020-08-01",
			EffectiveDateEnd: "2021-07-31",
			PresentTag: "1",
			PresentQuantity: "1",
		}
	}
	
	if o.OrderType == 4 {
		ordinaryProductC = OrdinaryProduct{
			ProductID:          "32573",
			ProductVersionID:   "56435",
			StandardPrice:      6000,
			MinimumPrice:       6000,
			SellingCount:       1,
			SellingAmount:      "0",
			EffectiveDateStart: "2022-08-01",
			EffectiveDateEnd:   "2023-07-31",
			PresentTag:         "0",
			PresentQuantity:    "0",
		}
	}
	reductionReason := "bee自动造数"

	//ordinaryProductList = append(ordinaryProductList, ordinaryProductA, ordinaryProductB, ordinaryProductC)
	ordinaryProductList = append(ordinaryProductList, ordinaryProductC)

	productInfo := ProductInfo{
		OrdinaryProductList: ordinaryProductList,
		ReductionReason:     reductionReason,
	}

	var invoiceBeforePaid string

	invoiceBeforePaid = "0"
	if o.OrderType == 5 {
		invoiceBeforePaid = "1"
	}

	invoiceInfo := InvoiceInfo{
		InvoiceType:      "4",
		InvoiceTitleType: "2",
		Title:            "bee自动造数开票抬头",
	}

	deliveryInfo := DeliveryInfo{
		ReceiverEmail:  "11@qq.com",
		ReceiverMobile: "13635534591",
	}

	contactInfo := ContactInfo{
		ContactName: "河北-测试-1",
		Phone:       "",
		Gender:      "0",
		Role:        "",
	}

	orderRemark := OrderRemark{
		SignerID:     "b763cad2fe2542018602d41fccc4ee39",
		OperatorType: "2",
		OperatorID:   "b763cad2fe2542018602d41fccc4ee39",
	}

	var accountDeductList  []AccountDeduct

	accountDeductA := AccountDeduct{
		AccountID:       o.AccountId,
		AccountType:     "CASH",
		TradeAmount:     100,
		AvailableAmount: 9900,
	}

	accountDeductList = append(accountDeductList, accountDeductA)
	IdentityId, _ := strconv.Atoi(o.IdentityId)

	balanceDeductInfo := BalanceDeductInfo{
		CustomerIdentityID: IdentityId,
		CouponDeductAmount:  0,
		AccountDeductAmount: 100,
		InvoiceAmount:       0,
		CouponDeductList:    nil,
		AccountDeductList:   accountDeductList,
	}

	var payInfo PayInfo
	var order OrdinaryOrder
	var nonePayOrder NonePayOrdinaryOrder
	var newOrder interface{}

	if o.OrderType == 5 {
		nonePayOrder = NonePayOrdinaryOrder{
			BaseInfo:          baseInfo,
			ProductInfo:       productInfo,
			InvoiceBeforePaid: invoiceBeforePaid,
			InvoiceInfo:       invoiceInfo,
			DeliveryInfo:      deliveryInfo,
			ContactInfo:       contactInfo,
			OrderRemark:       orderRemark,
		}
		newOrder = nonePayOrder
	} else  {
		payInfo = PayInfo{
			PaymentWay:        "2",
			BalanceDeductInfo: balanceDeductInfo,
		}
		order = OrdinaryOrder{
			BaseInfo:          baseInfo,
			ProductInfo:       productInfo,
			InvoiceBeforePaid: invoiceBeforePaid,
			InvoiceInfo:       invoiceInfo,
			DeliveryInfo:      deliveryInfo,
			ContactInfo:       contactInfo,
			OrderRemark:       orderRemark,
			PayInfo:           payInfo,
		}
		newOrder = order
	}

	if o.OrderType == 4 {
		nonePayOrder = NonePayOrdinaryOrder{
			BaseInfo:          baseInfo,
			ProductInfo:       productInfo,
			InvoiceBeforePaid: invoiceBeforePaid,
			InvoiceInfo:       invoiceInfo,
			DeliveryInfo:      deliveryInfo,
			ContactInfo:       contactInfo,
			OrderRemark:       orderRemark,
		}
		newOrder = nonePayOrder
	}

	cookie := util.SetCookie("sso-epctoken", o.Token)


	headers := make(map[string]string)
	headers["Content-Type"] = "application/json;charset=UTF-8"

	logging.ZapLogger.Infow("普通订单下单", "下单数据", newOrder, "traceId", o.TraceId)
	client := resty.New()
	resp, ordinaryOrderErr := client.R().EnableTrace().
		SetCookie(cookie).
		SetHeaders(headers).SetBody(newOrder).
		Post(url)

	if ordinaryOrderErr != nil {
		return "", ordinaryOrderErr
	}

	logging.ZapLogger.Infow("创建普通订单结果", "data", resp.String(), "traceId", o.TraceId)

	return resp.String(), nil
}